package activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import adapter.CipaiInfoAdapter;
import db.CipaiDatabaseHelper;
import entity.CipaiInfo;

import com.example.administrator.ci_edit.R;

/**
 * Created by hello world on 2017/7/12.
 */

public class PoemSelectActivity extends AppCompatActivity {
    //private String[] data = {"长相思","钗头凤","点绛唇","蝶恋花","喝火令","贺新郎","木兰花","满江红","调笑令"};
    private List<CipaiInfo> cipaiInfoList = new ArrayList<CipaiInfo>();

    // private JiangDatabaseHelper jiangDatabaseHelper;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        /*super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_poem_select);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PoemSelectActivity.this,android.R.layout.simple_list_item_1,data);
        ListView listView=(ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);*/
        initCipaiInfos();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_poem_select);
        CipaiInfoAdapter adapter = new CipaiInfoAdapter(PoemSelectActivity.this, R.layout.cipainfo_item, cipaiInfoList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        //点击listview中的一项的点击操作
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                CipaiInfo cipaiInfo = cipaiInfoList.get(position);

                //Toast.makeText(PoemSelectActivity.this,cipaiInfo.getName(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(PoemSelectActivity.this, PoemShowActivity.class);
                //
                int mengid = cipaiInfo.getId();
                intent.putExtra("extra_mengid", mengid);

                PoemSelectActivity.this.startActivity(intent);


            }
        });

    }

    //用来初始化词牌数据
    private void initCipaiInfos() {
        //测试用
      /*  CipaiInfo dingfengbo=new CipaiInfo("定风波");
        cipaiInfoList.add(dingfengbo);
        CipaiInfo rumengling=new CipaiInfo("如梦令");
        cipaiInfoList.add(rumengling);
        CipaiInfo zuihuayin=new CipaiInfo("醉花阴");
        cipaiInfoList.add(zuihuayin);
        CipaiInfo shuidiaogetou=new CipaiInfo("水调歌头");
        cipaiInfoList.add(shuidiaogetou);
        CipaiInfo yijianmei=new CipaiInfo("一剪梅");
        cipaiInfoList.add(yijianmei);*/


        //试试他的方法
        CipaiDatabaseHelper cipaiDatabaseHelper = new CipaiDatabaseHelper();

        Cursor cursor = cipaiDatabaseHelper.getAllCipaiName();

        //从数据库中导入
        /*jiangDatabaseHelper = new JiangDatabaseHelper(this,"cipai",null,2);
        try {

            jiangDatabaseHelper.createDataBase();
            jiangDatabaseHelper.openDataBase();

        } catch (IOException ioe) {

            throw new Error("Unable to create database");

        }
        //这一句有问题
        SQLiteDatabase db =jiangDatabaseHelper.getWritableDatabase();
        //查询表中数据
        Cursor cursor=db.query("cipai",null,null,null,null,null,null);*/






        /*
        SQLiteDatabase db = JiangDBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select name from cipai" , null);*/

        if (cursor.moveToFirst()) {
            //判断CipaiList是否为空；不为空就强行清空
            if (cipaiInfoList.isEmpty() == false) {
                cipaiInfoList.clear();
            }


            do {
                //遍历Cursor对象，取出数据并打印
                String name = cursor.getString(cursor.getColumnIndex("name"));
                //String enname =cursor.getString(cursor.getColumnIndex("enname"));
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                //添加到List中去
                CipaiInfo cipaiInfo = new CipaiInfo(name, id);

                cipaiInfoList.add(cipaiInfo);


                //打印
                //Log.d("PoenSelectActivity","Cipai name is"+name);
                //Log.d("PoenSelectActivity","Cipai enname is"+enname);
                //Log.d("PoenSelectActivity","Cipai id is"+id);
            } while (cursor.moveToNext());

            for (int i = 0; i < cipaiInfoList.size(); i++) {
                //Log.d("PoemSelectActivity",/*cipaiInfoList.get(i).getId()+*/cipaiInfoList.get(i).getName());
            }
        }
        cursor.close();
    }
}
